Skip to content

Commit 3ae984a

Browse files
author
Joan Goyeau
committed
Use guaranteed binary and source compatibility between releases of Scalafmt
1 parent 4979069 commit 3ae984a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,34 @@ FormatterFunc createFormat() throws Exception {
9292

9393
Class<?> optionCls = classLoader.loadClass("scala.Option");
9494
Class<?> configCls = classLoader.loadClass("org.scalafmt.config.Config");
95+
Class<?> scalafmtCls = classLoader.loadClass("org.scalafmt.Scalafmt");
9596

9697
Object either;
9798

9899
try {
99-
// scalafmt >= v0.7.0-RC1
100-
Method fromHocon = configCls.getMethod("fromHoconString", String.class, optionCls);
101-
Object fromHoconEmptyPath = configCls.getMethod("fromHoconString$default$2").invoke(null);
100+
// scalafmt >= 1.6.0
101+
Method parseHoconConfig = scalafmtCls.getMethod("parseHoconConfig", String.class);
102102

103103
String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
104104

105-
Object configured = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
105+
Object configured = parseHoconConfig.invoke(null, configStr);
106106
either = invokeNoArg(configured, "toEither");
107107
} catch (NoSuchMethodException e) {
108-
// In case of a NoSuchMethodException try old configuration API
109-
// scalafmt <= v0.6.8
110-
Method fromHocon = configCls.getMethod("fromHocon", String.class, optionCls);
111-
Object fromHoconEmptyPath = configCls.getMethod("fromHocon$default$2").invoke(null);
108+
// scalafmt >= v0.7.0-RC1 && scalafmt < 1.6.0
109+
Method fromHocon = configCls.getMethod("fromHoconString", String.class, optionCls);
110+
Object fromHoconEmptyPath = configCls.getMethod("fromHoconString$default$2").invoke(null);
112111

113112
String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
114-
either = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
113+
114+
Object configured = fromHocon.invoke(null, configStr, fromHoconEmptyPath);
115+
either = invokeNoArg(configured, "toEither");
115116
}
116117

117118
config = invokeNoArg(invokeNoArg(either, "right"), "get");
118119
}
119120
return input -> {
120121
Object resultInsideFormatted = formatMethod.invoke(null, input, config, emptyRange);
121-
String result = (String) formattedGet.invoke(resultInsideFormatted);
122-
return result;
122+
return (String) formattedGet.invoke(resultInsideFormatted);
123123
};
124124
}
125125
}

0 commit comments

Comments
 (0)